﻿using Entitys.Models.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Common.Result
{
    public abstract class ErrorEnumeration : IComparable
    {
        public abstract string ErrorArea { get; }

        public string ErrorName { get; private set; }

        public int ErrorCode { get; private set; }

        public ErrorEnumeration(int errorCode, string errorName) => (ErrorCode, ErrorName) = (errorCode, errorName);

        public override string ToString() => ErrorName;

        protected abstract string Translate<T>() where T : ErrorEnumeration;

        public static IEnumerable<T> GetAll<T>() where T : ErrorEnumeration =>
            typeof(T).GetFields(BindingFlags.Public |
                                BindingFlags.Static |
                                BindingFlags.DeclaredOnly)
                     .Select(f => f.GetValue(null))
                     .Cast<T>();

        public override bool Equals(object? obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (obj is not ErrorEnumeration otherValue)
            {
                return false;
            }
            var typeMatches = GetType().Equals(obj.GetType());
            var valueMatches = ErrorCode.Equals(otherValue.ErrorCode);

            return typeMatches && valueMatches;
        }

        public int CompareTo(object? other)
        {
            return ErrorCode.CompareTo(((ErrorEnumeration)other!).ErrorCode);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
